package cn.wizzer.app.user.modules.service.impl;

import cn.wizzer.app.user.modules.models.UserCertify;
import cn.wizzer.app.user.modules.services.UserCertifyService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;

@IocBean(args = "refer:dao")
@Service(interfaceClass = UserCertifyService.class)
public class UserCertifyServiceImpl extends BaseServiceImpl<UserCertify> implements UserCertifyService {

    public UserCertifyServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Pagination listPage(int pageNumber, int pageSize, String searchKeyword, String[] searchDate, String status) {
        Sql sql = Sqls.create("select u.userName, u.nickName, u.phone, c.* from user_certify c ");
        Sql countSql = Sqls.create("select count(1) from user_certify c ");
        Sql joinSql = Sqls.create("left join user u on u.id = c.userId " +
                "$condition ");
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(searchKeyword)) {
            searchKeyword = "%" + searchKeyword + "%";
            cnd.and(Cnd.exps("u.nickName", "LIKE", searchKeyword)
                    .or("u.userName", "LIKE", searchKeyword)
                    .or("u.phone", "LIKE", searchKeyword)
                    .or("c.realName", "LIKE", searchKeyword)
                    .or("c.id", "LIKE", searchKeyword));
        }
        if (searchDate != null && searchDate.length != 0) {
            cnd.and("c.ctAt", ">=", Times.d2TS(Times.D(searchDate[0])));
            cnd.and("c.ctAt", "<=", Times.d2TS(Times.D(searchDate[1])));
        }
        if (Strings.isNotBlank(status)) {
            cnd.and("c.status", "=", status);
        }
        joinSql.setCondition(cnd);
        sql.appendSourceSql(joinSql.toString());
        sql.appendSourceSql("order by c.ctAt desc");
        countSql.appendSourceSql(joinSql.toString());
        return this.listPage(pageNumber, pageSize, sql, countSql);
    }
}
